package com.xiaowen.projectDataCode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaowen.codeData.entity.CodeData;
import com.xiaowen.projectDataCode.entity.ProjectDataCode;
import com.xiaowen.projectDataCode.mapper.ProjectDataCodeMapper;
import com.xiaowen.projectDataCode.service.ProjectDataCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目数据编码表 服务实现类
 * </p>
 *
 * @author yf-wenhao
 * @since 2024-10-03
 */
@Service
public class ProjectDataCodeServiceImpl extends ServiceImpl<ProjectDataCodeMapper, ProjectDataCode> implements ProjectDataCodeService {

    @Autowired
    private ProjectDataCodeMapper projectDataCodeMapper;

    @Override
    public Map<Integer, List<ProjectDataCode>> queryProjectDataCodeMap(Integer projectId) {
        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(projectId);
        projectDataCode.setDeleteFlag(false);
        queryWrapper.setEntity(projectDataCode);
        List<ProjectDataCode> projectDataCodeList = projectDataCodeMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(projectDataCodeList)){
            return projectDataCodeList.stream().collect(Collectors.groupingBy(ProjectDataCode::getProjectDataId));
        }
        return null;
    }

    @Override
    public void deleteByProjectDataId(Integer projectId, List<Integer> dataIdList) {
        ProjectDataCode entity = new ProjectDataCode();
        entity.setDeleteFlag(true);
        entity.setUpdateTime(new Date());

        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(projectId);
        projectDataCode.setDeleteFlag(false);
        queryWrapper.setEntity(projectDataCode);
        queryWrapper.in("project_data_id", dataIdList);
        projectDataCodeMapper.update(entity, queryWrapper);
    }

    @Override
    public List<ProjectDataCode> queryDataCodeByCode(Integer projectId, List<Integer> codeList) {
        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(projectId);
        projectDataCode.setDeleteFlag(false);
        queryWrapper.setEntity(projectDataCode);
        queryWrapper.in("code", codeList);
        return projectDataCodeMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProjectDataCode> queryDataCodeByDataId(Integer projectId, List<Integer> dataIdList) {
        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(projectId);
        projectDataCode.setDeleteFlag(false);
        queryWrapper.setEntity(projectDataCode);
        queryWrapper.in("project_data_id", dataIdList);
        return projectDataCodeMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteByIds(List<Integer> idList) {
        ProjectDataCode entity = new ProjectDataCode();
        entity.setDeleteFlag(true);
        entity.setUpdateTime(new Date());

        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList);
        projectDataCodeMapper.update(entity, queryWrapper);
    }

    @Override
    public void updateByCode(CodeData reqDTO) {
        ProjectDataCode entity = new ProjectDataCode();
        entity.setName(reqDTO.getName());
        entity.setDeleteFlag(reqDTO.getDeleteFlag());
        entity.setUpdateTime(new Date());
        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(reqDTO.getProjectId());
        projectDataCode.setCode(reqDTO.getCode());
        queryWrapper.setEntity(projectDataCode);
        projectDataCodeMapper.update(entity, queryWrapper);
    }

    @Override
    public void deleteByDataCode(Integer projectId, Integer code) {
        ProjectDataCode entity = new ProjectDataCode();
        entity.setDeleteFlag(true);
        entity.setUpdateTime(new Date());

        QueryWrapper<ProjectDataCode> queryWrapper = new QueryWrapper<>();
        ProjectDataCode projectDataCode = new ProjectDataCode();
        projectDataCode.setProjectId(projectId);
        projectDataCode.setDeleteFlag(false);
        projectDataCode.setCode(code);
        queryWrapper.setEntity(projectDataCode);
        projectDataCodeMapper.update(entity, queryWrapper);
    }
}
